try { var dummy = TextLogger; } catch ( e ) {

function TextLogger(){
	// Do nothing. Use the init method instead.
}

// Required for AS 1 inheritance.
TextLogger.prototype = new RTCFComponent();

TextLogger.prototype.separator = ",";
TextLogger.prototype.clientSeparator = "|";
TextLogger.prototype.clientProperties = ["ip", "id", "agent", "referrer", "pageUrl", "protocol"];
TextLogger.prototype.shortMonthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

/** Setup initial values and listeners. */
TextLogger.prototype.init = function(){
	//trace("TextLogger.prototype.init>");
	// Build a date string for the new log file name
	var now = new Date();
	var dateString = now.getFullYear() + "_" + this.pad2(now.getMonth()) + "_" + this.pad2(now.getDate()) + "_" + this.pad2(now.getHours()) + 
			"_" + this.pad2(now.getMinutes()) + "_" + this.pad2(now.getSeconds()) + "_" + now.getMilliseconds();
	
	// Build the file path from the RTCFComponent's file path and the file name
	var filePath = this._filePath_ + "textLog_" + dateString + ".txt"
	
	// Keep the directory path alone to create the directory path
	var dirPath = this._filePath_;
	var dirArray = dirPath.split("/");

	// Create the directory for the log file if necessary
	var totalPath = "";
	dir = new File();
	for (var i = 0; i < dirArray.length; i++){
		var subPath = dirArray[i];
		if (subPath.length > 0) {
			totalPath += "/" + subPath;
			// If the directory doesn't exist create it
			if (!(new File(totalPath).isDirectory)) dir.mkdir(totalPath);
		}		
	}
	
	// Now that there is a valid directory create the actual log file
	this.file = new File(filePath);
	if (this.file){
		this.file.open("text", "append");
	}

	// Listen for any application events this component is interested in.
	// Mayebe get this information from an event array the user can set?
	// For now these two are hard wired
	this.rtcf.addListener("onConnect", this);
	this.rtcf.addListener("onDisconnect", this);
}


TextLogger.prototype.onConnect = function(client){
	this.log(2, "onConnect> number of arguments: " + arguments.length, client, this.argumentsToObject(arguments));
}

TextLogger.prototype.onDisconnect = function(client ){
	this.log(2, "onDisconnect> number of arguments: " + arguments.length, client, this.argumentsToObject(arguments));
}

TextLogger.prototype.log = function(level, message, client, object){
	var s = this.separator;
	var now = new Date();
	var dateString = this.pad2(now.getDate()) + "/" + this.shortMonthNames[now.getMonth()] + "/" + now.getFullYear() + ":" + this.pad2(now.getHours()) + ":" + this.pad2(now.getMinutes()) + ":" + this.pad2(now.getSeconds()) + " " + -1 * (now.getTimezoneOffset()/60);
	if (client instanceof Client){
		this.file.writeln(dateString + s + level + s + message + s + this.clientToString(client) + s + this.objectToString(object));
	}
	else {
		this.file.writeln(dateString + s + level + s + message + s + s + this.objectToString(object));
	}
}

TextLogger.prototype.pad2 = function(str){
	str += ""; 
	if (str.length == 0) return "00";
	if (str.length == 1) return "0" + str;
	return str;
}

TextLogger.prototype.clientToString = function(client){
	var s = this.separator;
	var ar = [];
	for (var i = 0; i < this.clientProperties.length; i++){
		ar.push(client[this.clientProperties[i]]);
	}
	return ar.join(s);
}

TextLogger.prototype.objectToString = function(object){
	var s = this.separator;
	var ar = [];
	for (var p in object){
		ar.push(p + ": " + object[p]);
	}
	return ar.join(s);
}

TextLogger.prototype.argumentsToObject = function(args){
	var s = this.separator;
	var obj = {length: args.length};
	for (var i = 0; i < args.length; i++){
		obj[i + ""] = args[i];
	}
	return obj;
}

} // end catch
